package org.weasel.unittests;

import java.sql.Time;
import java.util.ArrayList;

import junit.framework.TestCase;

import org.weasel.business.Developer;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.Unscheduled;
import org.weasel.business.UserStory;
import org.weasel.business.Utilities;
import org.weasel.data.ProjectManager;

public class ReleaseTest extends TestCase {
	private final String xmlFileLoc = "./testfolder/ReleaseTest.xml";
	private Release release;
	private Project project;
	private ProjectManager manager;
	private ArrayList<Developer> allDevelopers;
	private Developer developer;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		developer = new Developer("9999999", "developer", "mail", "position");
		allDevelopers = new ArrayList<Developer>();
		allDevelopers.add(developer);
		project = new Project(Utilities.createUniqueId(), "my project", xmlFileLoc, allDevelopers);
		manager = project.getProjectManager();
		release = project.createAndAddRelease("1st release"); //create a release by adding it to the project
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		developer = null;
		allDevelopers = null;
		manager = null;
		release = null;
		project = null;
		Utilities.deleteFile(xmlFileLoc);
	}

	public void testIterations() throws Exception {
		Iteration iteration = release.createAndAddIteration("1st iteration");
		
		//check if iteration is really in XML file
		Iteration iterCheck = manager.getIteration(iteration.getId());
		
		assertEquals("expected iteration (from XML) with id " + iteration.getId(), iteration.getId(), iterCheck.getId());
		assertEquals("expected iteration id " + iteration.getId(), iteration.getId(), release.getIteration(iteration.getId()).getId());
	}

	public void testRemoveIteration() throws Exception {
		boolean ok = false;
		Iteration iteration = release.createAndAddIteration("1st iteration");
		release.removeIteration(iteration.getId());
		
		//check if iteration is no more in XML file
		try{
			manager.getIteration(iteration.getId());
		}catch(NullPointerException e){
			ok = true;
		}
		assertEquals("expected true since iteration should be deleted (in XML)", true, ok);
		assertEquals("Expected null since no iteration should be retrieved!", null, release.getIteration(iteration.getId()));
	}
	
	public void testUnscheduledStories() throws Exception{
		UserStory story = release.addUnscheduledStory("my story", 5, Time.valueOf("13:00:00"), Time.valueOf("14:00:00"), 5);
		
		//check if it is present in the XML file
		UserStory storyCheck = manager.getUserStory(story.getId());
		assertEquals("expected story (from XML) with id " + story.getId(), story.getId(), storyCheck.getId());
		
		//should also be in the unscheduled part (of the XML) of the release
		Unscheduled unsch = manager.getUnscheduled(release.getUnscheduled().getId());
		UserStory unschStory = unsch.getUnscheduledUserStory(story.getId());
		assertEquals("expected unscheduled user story with id " + story.getId(), story.getId(), unschStory.getId());
		
		assertEquals("expected story id " + story.getId(), story.getId(), release.getUnscheduledStory(story.getId()).getId());
		assertEquals("expected story name 'my story'", story.getName(), release.getUnscheduledStory(story.getId()).getName());
	}
	
	public void testRemoveUnscheduledStory() throws Exception{
		boolean ok = false;
		UserStory story = release.addUnscheduledStory("my story", 5, Time.valueOf("13:00:00"), Time.valueOf("14:00:00"), 5);
		
		release.removeUnscheduledStory(story.getId());
		
		//should be present any more in the XML-file
		try{
			manager.getUserStory(story.getId());
		}catch(NullPointerException e){
			ok = true;
		}
		assertEquals("expected true since story shouldn't be present any more", true, ok);
		assertEquals("expected a null value", null, release.getUnscheduledStory(story.getId()));
	}

}
